// AddUserDialog.cpp : implementation file
//

#include "stdafx.h"
#include "noob_vc.h"
#include "AddUserDialog.h"
#include "noob_vcDlg.h"
#include "DataManger.h"//proxy preference store/read

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAddUserDialog dialog

extern string CString2String(CString& vcSql);
CAddUserDialog::CAddUserDialog(CWnd* pParent /*=NULL*/)
	: CDialog(CAddUserDialog::IDD, pParent),ptwitterObj(NULL)
{
	//{{AFX_DATA_INIT(CAddUserDialog)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	
}


void CAddUserDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAddUserDialog)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CAddUserDialog, CDialog)
	//{{AFX_MSG_MAP(CAddUserDialog)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAddUserDialog message handlers

void CAddUserDialog::OnButton2() //add user
{
	// TODO: Add your control notification handler code here
	initTwitCurl();
}

void CAddUserDialog::OnButton1() //cancel
{
	// TODO: Add your control notification handler code here
	OnCancel();
}

void CAddUserDialog::initTwitCurl()
{
	
	std::string tmpStr( "" );
	//std::string replyMsg( "" );

	if( myOAuthAccessTokenKey.size() && myOAuthAccessTokenSecret.size() )
	{
		/* If we already have these keys, then no need to go through auth again */
		printf( "\nUsing:\nKey: %s\nSecret: %s\n\n", myOAuthAccessTokenKey.c_str(), myOAuthAccessTokenSecret.c_str() );

		ptwitterObj->getOAuth().setOAuthTokenKey( myOAuthAccessTokenKey );
		ptwitterObj->getOAuth().setOAuthTokenSecret( myOAuthAccessTokenSecret );
	}
	else
	{
		CString pbuffer;
		GetDlgItemText(IDC_EDIT3,pbuffer);
		/* Step 2: Get request token key and secret */
		if(pbuffer.GetLength() <= 0)
		{
			ptwitterObj->oAuthRequestToken( tmpStr );
			ShellExecuteA(NULL, "open", tmpStr.c_str(), NULL, NULL, SW_SHOWNORMAL);

			return;


		}
		else
		{
			/* Step 3: Ask user to visit web link and get PIN */
			char szOAuthVerifierPin[1024];
			memset( szOAuthVerifierPin, 0, 1024 );
			printf( "\nPlease visit this link in web browser and authorize this application:\n%s", tmpStr.c_str() );
			printf( "\nEnter the PIN provided by twitter: " );
			
			
			//strcpy(szOAuthVerifierPin,lineEdit_3->text().toUtf8().constData());
			
			strcpy(szOAuthVerifierPin,CString2String(pbuffer).c_str());

			tmpStr = szOAuthVerifierPin;
			ptwitterObj->getOAuth().setOAuthPin( tmpStr );

			/* Step 4: Exchange request token with access token */
			ptwitterObj->oAuthAccessToken();

			/* Step 5: Now, save this access token key and secret for future use without PIN */
			ptwitterObj->getOAuth().getOAuthTokenKey( myOAuthAccessTokenKey );
			ptwitterObj->getOAuth().getOAuthTokenSecret( myOAuthAccessTokenSecret );

			/* Step 6: Save these keys in a file or wherever */
			ofstream oAuthTokenKeyOut;
			ofstream oAuthTokenSecretOut;

			oAuthTokenKeyOut.open( "twitterClient_token_key.txt" );
			oAuthTokenSecretOut.open( "twitterClient_token_secret.txt" );

			oAuthTokenKeyOut.clear();
			oAuthTokenSecretOut.clear();

			oAuthTokenKeyOut << myOAuthAccessTokenKey.c_str();
			oAuthTokenSecretOut << myOAuthAccessTokenSecret.c_str();

			oAuthTokenKeyOut.close();
			oAuthTokenSecretOut.close();
		}

	}
	/* OAuth flow ends */

	/* Set twitter username and password */


	CString pbuffer;
	GetDlgItemText(IDC_EDIT2,pbuffer);
	tmpStr = CString2String(pbuffer);
	ptwitterObj->setTwitterUsername( tmpStr );

	GetDlgItemText(IDC_EDIT3,pbuffer);
	tmpStr = CString2String(pbuffer);
	ptwitterObj->setTwitterPassword( tmpStr );
	
	CNoob_vcDlg* pmain = (CNoob_vcDlg*)(AfxGetMainWnd());
	if(pmain)
	{
		pmain->twits.push_back(ptwitterObj);
		ptwitterObj = NULL;
	}
		
	OnOK();


}
BOOL CAddUserDialog::OnInitDialog()
{
	CDialog::OnInitDialog();

	// TODO:  Add extra initialization here
	ptwitterObj = new twitCurl;
	
	std::string tmpStr( "" );

	/* OAuth flow begins */
	/* Step 0: Set OAuth related params. These are got by registering your app at twitter.com */
	ptwitterObj->getOAuth().setConsumerKey( std::string( "XvmqJmIsNFHHFjQVhhvhNA" ) );
	ptwitterObj->getOAuth().setConsumerSecret( std::string( "TZSsIiBiP23GGtgN74tYyJJIEhuohUUcsufxWUkZQ" ) );

	/* Step 1: Check if we alredy have OAuth access token from a previous run */
	char szKey[1024];
	myOAuthAccessTokenKey = "";
	myOAuthAccessTokenSecret = "";
	ifstream oAuthTokenKeyIn;
	ifstream oAuthTokenSecretIn;

	oAuthTokenKeyIn.open( "twitterClient_token_key.txt" );
	oAuthTokenSecretIn.open( "twitterClient_token_secret.txt" );

	memset( szKey, 0, 1024 );
	oAuthTokenKeyIn >> szKey;
	myOAuthAccessTokenKey = szKey;

	memset( szKey, 0, 1024 );
	oAuthTokenSecretIn >> szKey;
	myOAuthAccessTokenSecret = szKey;

	oAuthTokenKeyIn.close();
	oAuthTokenSecretIn.close();

	//* Set proxy server usename, password, IP and port (if present) 
	if( ::atoi(CDataManger::isProxyOn.c_str())){
		ptwitterObj->setProxyServerIp( CDataManger::ip );
		ptwitterObj->setProxyServerPort( CDataManger::port);

		if (CDataManger::name.length()){
			ptwitterObj->setProxyUserName( CDataManger::name );
			ptwitterObj->setProxyPassword( CDataManger::pwd );
		}
		
	}
	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

CAddUserDialog::~CAddUserDialog()
{
	if(ptwitterObj)
		delete ptwitterObj;
}